package com.qdlc.p2p.manage.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ModelDriven;
import com.qd.p2p.sina.pay.response.QueryVerifyResponse;
import com.qd.p2p.sina.pay.response.ShowMemberInfosSinaResponse;
import com.qd.p2p.sina.service.SinaPayService;
import com.qdlc.p2p.biz.service.UserCacheService;
import com.qdlc.p2p.biz.service.UserIdentifyService;
import com.qdlc.p2p.biz.util.excel.ExcelUtils;
import com.qdlc.p2p.biz.util.excel.JsGridReportBase;
import com.qdlc.p2p.biz.util.excel.TableData;
import com.qdlc.p2p.biz.web.action.BaseAction;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.checkrule.RealNameAttestationRuleCheck;
import com.qdlc.p2p.dal.common.RuleCheckHelper;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.common.model.jpa.QueryParam;
import com.qdlc.p2p.dal.common.model.jpa.SearchFilter;
import com.qdlc.p2p.dal.common.model.jpa.SearchFilter.Operators;
import com.qdlc.p2p.dal.dao.UserDao;
import com.qdlc.p2p.dal.dto.Operator;
import com.qdlc.p2p.dal.dto.User;
import com.qdlc.p2p.dal.dto.UserCache;
import com.qdlc.p2p.dal.dto.UserIdentify;
import com.qdlc.p2p.dal.model.UserIdentifyModel;
import com.qdlc.p2p.dal.model.UserModel;
import com.qdlc.p2p.dal.util.UserUtils;

/**
 * 认证用户管理
 * 
 * @author zf
 * @version 2.0
 * @since 2014年5月16日
 */
public class ManageUserIdentifyAction extends BaseAction<UserIdentify> implements ModelDriven<UserIdentify> {

	@Resource
	private UserIdentifyService userIdentifyService;
	@Resource
	private UserCacheService userCacheService;
	@Resource
	private UserDao userDao;
	@Autowired
	SinaPayService sinaPayService;

	private Operator operator;

	private Map<String, Object> data;
	
	private UserIdentifyModel model = new UserIdentifyModel();
	
	public UserIdentifyModel getModel() {
		return model;
	}

	/**
	 * 认证查询
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action(value = "/modules/user/user/userIdentify/userIdentifyManager")
	public String userIdentifyManager() throws Exception {
		return "userIdentifyManager";
	}

	/**
	 * 认证查询
	 * 
	 * @throws Exception
	 */
	@Action(value = "/modules/user/user/userIdentify/userIdentifyList")
	public void userIdentifyList() throws Exception {
		data = new HashMap<String, Object>();
		int pageNumber = paramInt("page");// 当前页数
		int pageSize = paramInt("rows");// 每页总数
		if (paramString("emailStatus").equals("")) {
			model = null;
		}
		searchName = paramString("searchName");
		PageDataList<UserIdentifyModel> pagaDataList = userIdentifyService.findUserIdentifylist(pageNumber, pageSize,model, searchName);
		int totalPage = pagaDataList.getPage().getTotal();// 总页数
		data.put("total", totalPage);
		data.put("rows", pagaDataList.getList());
		printJson(getStringOfJpaObj(data));
	}

	/**
	 * 新浪认证查询
	 *
	 * @return
	 * @throws Exception
	 */
	@Action(value = "/modules/user/user/userIdentify/sinaUserIdentifyManager")
	public String sinaUserIdentifyManager() throws Exception {
		return "sinaUserIdentifyManager";
	}

	/**
	 * 新浪认证查询
	 *
	 * @throws Exception
	 */
	@Action(value = "/modules/user/user/userIdentify/sinaUserIdentifyList")
	public void sinaUserIdentifyList() throws Exception {


		searchName = paramString("searchName");
		String identityId = paramString("tppUserCustId");

		if (StringUtil.isBlank(identityId) && StringUtil.isNotBlank(searchName)) {
			QueryParam param = QueryParam.getInstance();
			SearchFilter orFilter1 = new SearchFilter("userName", Operators.LIKE, searchName);
			SearchFilter orFilter2 = new SearchFilter("realName", Operators.LIKE, searchName);
			param.addOrFilter(orFilter1, orFilter2);
			List<User> user = userDao.findByCriteria(param);
			if (user.size() > 0) {
				identityId = UserUtils.getUserTppCustId(user.get(0).getUserId());
			}
		}

		data = new HashMap<String, Object>();
		PageDataList<UserIdentifyModel> pagaDataList = new PageDataList<UserIdentifyModel>();
		if (StringUtil.isNotBlank(identityId)) {
			QueryVerifyResponse queryVerifyRes = sinaPayService.queryVerify(identityId);// 查询新浪认证信息
			ShowMemberInfosSinaResponse showMemberInfosSinaRes = sinaPayService.showMemberInfosSina(identityId);// sina页面展示用户信息

			int pageNumber = paramInt("page");// 当前页数
			int pageSize = paramInt("rows");// 每页总数
			if (paramString("emailStatus").equals("")) {
				model = null;
			}
			searchName = paramString("searchName");
//			pagaDataList = userIdentifyService.findUserIdentifylist(pageNumber, pageSize,model, searchName,identityId,queryVerifyRes,showMemberInfosSinaRes);
			int totalPage = pagaDataList.getPage().getTotal();// 总页数
			data.put("total", totalPage);
			data.put("rows", pagaDataList.getList());
			printJson(getStringOfJpaObj(data));
		} else {
			int pageNumber = paramInt("page");// 当前页数
			int pageSize = paramInt("rows");// 每页总数
//			pagaDataList = userIdentifyService.findUserIdentifylist(pageNumber, pageSize,model, searchName,identityId);
			data.put("rows", pagaDataList.getList());
			printJson(getStringOfJpaObj(data));
		}
	}

	/**
	 * 获得用户认证信息页面
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action(value = "/modules/user/user/userIdentify/userRealNameManager")
	public String userRealNameManager() throws Exception {
		return "userRealNameManager";
	}

	/**
	 * 获得用户认证信息列表
	 * 
	 * @throws Exception
	 */
	@Action(value = "/modules/user/user/userIdentify/userRealNameList")
	public void userRealNameList() throws Exception {
		int pageNumber = paramInt("page");// 当前页数
		int pageSize = paramInt("rows");// 每页总数
		String searchName = paramString("searchName");
		String realNameStatus = paramString("realNameStatus");
		if (StringUtil.isBlank(realNameStatus)&&StringUtil.isBlank(searchName)) {
			model.setRealNameStatus(2);
		}
		PageDataList<UserModel> pagaDataList = userIdentifyService.userModelList(pageNumber, pageSize, model, searchName);
		int totalPage = pagaDataList.getPage().getTotal();// 总页数
		data = new HashMap<String, Object>();
		data.put("total", totalPage);
		data.put("rows", pagaDataList.getList());
		printJson(getStringOfJpaObj(data));
	}
	
	/**
	 * 导出认证记录列表
	 * 
	 * @throws Exception
	 */
	@Action("/modules/user/user/userIdentif/exportExcelUser")
	public void exportExcelUser() throws Exception {
		response.setContentType("application/msexcel;charset=UTF-8");
		model.setPage(1);
		model.setRows(5000);
		// 获取记录
		PageDataList<UserIdentifyModel> pageDataList = userIdentifyService.findUserIdentifylist(model);
		
		String title = "用户认证记录Excel表";
		
		String[] hearders = new String[] { "编号", "用户名", "真实姓名", "邮箱认证","邮箱", "手机认证", "手机号码", "实名认证", "实名认证审核时间" }; // 表头数组
		String[] fields = new String[] { "id", "userName", "realName", "emailStatusString", "email", "mobilePhoneStatusString",
				"mobilePhone", "realNameStatusString", "realNameVerifyTime" };// 对象属性数组
		TableData td = ExcelUtils.createTableData(pageDataList.getList(), ExcelUtils.createTableHeader(hearders), fields);
		JsGridReportBase report = new JsGridReportBase(request, response);
		report.exportToExcel(title, getOperator().getUserName(), td);
	}

	/**
	 * <p>审核用户认证信息页面</p>
	 * @author 原系统 2016年2月4日 下午4:42:02
	 * @return
	 * @throws Exception
	 * @modificationHistory=========================逻辑或功能性重大变更记录
	 * @modify by user: Kaiju Zhao 2016年2月4日 下午4:42:02
	 * @modify by reason: 消除错误
	 */
	@Action(value = "/modules/user/user/userIdentify/userRealNameEditPage")
	public String userRealNameEditPage() throws Exception {
		UserIdentify userIdentify = userIdentifyService.findById(model.getId());
		UserCache userCache = userCacheService.findByUserId(userIdentify.getUserId());
		// 实名(身份证)校验
		RealNameAttestationRuleCheck realCardCheck = RuleCheckHelper.getValue(RealNameAttestationRuleCheck.class);
		if (!realCardCheck.style().equals("ID5")) {
			request.setAttribute("showPic", "showPic");
		}
		request.setAttribute("userIdentify", userIdentify);
		request.setAttribute("userCache", userCache);
		return "userRealNameEditPage";
	}

	/**
	 * 审核用户认证信息
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action(value = "/modules/user/user/userIdentify/userRealNameEdit")
	public void userRealNameEdit() throws Exception {
		operator = getOperator();
		data = new HashMap<String, Object>();
		long id = paramLong("id");
		String realNameVerifyRemark = paramString("realNameVerifyRemark");
		int realNameStatus = paramInt("realNameStatus");
		userIdentifyService.userAttestationEdit(id, realNameVerifyRemark, realNameStatus, operator);
		data.put("result", true);
		data.put("msg", "操作成功！");
		printJson(getStringOfJpaObj(data));
	}

}
